import React, { PropTypes, PureComponent } from 'react';
import { connect } from 'dva';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message } from 'antd';
import { TableC, InputNumberC } from '../../comps';

@Form.create()
class TableItem extends PureComponent {
  state = {};

  componentDidMount() {
    // 父组件对本组件的引用
    this.props.onRef(this);
  }

  UNSAFE_componentWillReceiveProps(nextProps) {}

  componentWillUnmount() {}

  // 获取表单数据，进行提交
  getData = (e) => {
    const {
      form: { validateFields },
    } = this.props;
    let query = undefined;
    validateFields((err, fieldsValue) => {
      if (!err) {
        query = fieldsValue;
      }
    });
    return query;
  };

  //改变表格列
  changeColumns = (arr) => {
    arr &&
      arr.map((item) => {
        if (item.key == 'low' || item.key == 'medium' || item.key == 'high') {
          item.render = (text, row) => {
            if (!this.props.isEdit) {
              return text.toFixed(2) + '%';
            } else {
              return (
                <Form>
                  <InputNumberC
                    initialValue={text}
                    unit='%'
                    precision={2}
                    step={0.01}
                    form={this.props.form}
                    dataIndex={row.stressTestType + '_' + row.code + '_' + item.key}
                    rules={[{ required: true, message: `请填写数值` }]}
                  />
                </Form>
              );
            }
          };
        }
      });
    return arr;
  };

  render() {
    const { columns, dataSource, loading } = this.props;
    const newColumns = [...this.changeColumns(columns)];
    return (
      <div style={{ marginBottom: '12px' }}>
        <TableC loading={loading.effects['sceneSettingsM/stressTestConfig'] == true} columns={newColumns} dataSource={dataSource} hidePage={true} rowKey='id' />
      </div>
    );
  }
}
export default connect(({ sceneSettingsM, loading }) => ({ sceneSettingsM, loading }))(TableItem);
